﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;

namespace MINERP.BLL.Common
{
    public class PaperWasteRecordsBll
    {
        public List<PaperWasteRecord_info> GetListWaste(int inWasteId)
        {
            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                PaperWasteRecords paperWasteRecord = db.PaperWasteRecords.FirstOrDefault(p => p.Id == inWasteId);
                List<PaperWasteRecords> listRecod =
                    db.PaperWasteRecords.Where(p => p.PaperInfo.PrintPaperId == paperWasteRecord.PaperInfo.PrintPaperId)
                        .ToList();
                PaperWasteRecord_info info;
                foreach (PaperWasteRecords list in listRecod)
                {
                    info = new PaperWasteRecord_info();
                    info.CreateTime = UnityMethod.GetSystemTime();
                    info.Id = list.Id;
                    info.Length = list.Length;
                    if (!list.PaperInfoReference.IsLoaded)
                    {
                        list.PaperInfoReference.Load();
                    }
                    info.PaperInfo = new MINERP.MODEL.PaperManager.PaperInfo_Info() { AddTime = (DateTime)list.PaperInfo.AddTime, EndPaperLength = (double)list.PaperInfo.EndPaperLength, InvalidationArea = list.PaperInfo.PaperBreakArea, LastBordId = list.PaperInfo.LastBordId, LastLength = (double)list.PaperInfo.LastLength, LastPrintTime = info.PaperInfo.LastPrintTime, Length = (double)list.PaperInfo.Length, PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength, PaperEndArea = list.PaperInfo.PaperEndArea };
                    // info.PaperWasteTypeId = list.PaperWasteType.Id;
                    if (!list.PaperWasteTypeReference.IsLoaded)
                    {
                        list.PaperWasteTypeReference.Load();
                        //list.PaperWasteTypeReference.Load();
                    }
                    info.PaperWasteType = new PaperWasteType_info() { Id = list.PaperWasteType.Id, PaperOperationTypeName = list.PaperWasteType.PaperOperationTypeName };
                    info.Remark = list.Remark;
                    info.Status = list.Status;
                    info.Width = list.Width;
                    // info.Length
                    rtnList.Add(info);

                }
            }
            return rtnList;
        }
        public List<PaperWasteRecord_info> MakeSureRecord(int inWasteId)
        {

            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                PaperWasteRecords paperWasteRecord = db.PaperWasteRecords.FirstOrDefault(p => p.Id == inWasteId);
                if (paperWasteRecord==null)
                {
                    return new List<PaperWasteRecord_info>();
                }
                paperWasteRecord.Status = (int)WasterPaperStatus.Sure;
                paperWasteRecord.PaperInfoReference.Load();
                List<PaperWasteRecords> listRecod =
                    db.PaperWasteRecords.Where(p => p.PaperInfo.PrintPaperId == paperWasteRecord.PaperInfo.PrintPaperId)
                        .ToList();
                PaperWasteRecord_info info;
                foreach (PaperWasteRecords list in listRecod)
                {
                    info = new PaperWasteRecord_info();
                    info.CreateTime = UnityMethod.GetSystemTime();
                    info.Id = list.Id;
                    info.Length = list.Length;
                    if (!list.PaperInfoReference.IsLoaded)
                    {
                        list.PaperInfoReference.Load();
                    }
                    //info.PaperInfo = new PaperInfo_info() { AddTime = list.PaperInfo.AddTime, EndPaperLength = list.PaperInfo.EndPaperLength, InvalidationArea = list.PaperInfo.InvalidationArea, LastBordId = list.PaperInfo.LastBordId, LastLength = list.PaperInfo.LastLength, LastPrintTime = info.PaperInfo.LastPrintTime, Length = list.PaperInfo.Length, PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength, PaperEndArea = list.PaperInfo.PaperEndArea };
                    // info.PaperWasteTypeId = list.PaperWasteType.Id;
                    //info.PaperInfo=new PaperInfo_info();
                    MINERP.MODEL.PaperManager.PaperInfo_Info paperinfo = new MINERP.MODEL.PaperManager.PaperInfo_Info();

                    {
                        paperinfo.AddTime = (DateTime)list.PaperInfo.AddTime;
                        paperinfo.EndPaperLength = (double)list.PaperInfo.EndPaperLength;
                        paperinfo.InvalidationArea = (double)list.PaperInfo.PaperBreakArea;
                        paperinfo.LastBordId = list.PaperInfo.LastBordId;
                        paperinfo.LastLength =(double) list.PaperInfo.LastLength;
                        paperinfo.LastPrintTime = (DateTime)list.PaperInfo.LastPrintTime;
                        paperinfo.Length = (double)list.PaperInfo.Length;
                        ;
                        paperinfo.PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength;

                        paperinfo.PaperEndArea = list.PaperInfo.PaperEndArea;
                        paperinfo.PrintPaperId = list.PaperInfo.PrintPaperId;
                    }
                    info.PaperInfo = paperinfo;
                    if (!list.PaperWasteTypeReference.IsLoaded)
                    {
                        list.PaperWasteTypeReference.Load();
                    }
                    info.PaperWasteType = new PaperWasteType_info() { Id = list.PaperWasteType.Id, PaperOperationTypeName = list.PaperWasteType.PaperOperationTypeName };
                    info.Remark = list.Remark;
                    info.Status = list.Status;
                    info.Width = list.Width;
                    // info.Length
                    db.SaveChanges();
                    rtnList.Add(info);

                }
            }
            return rtnList;
        }
        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="inWasteId"></param>
        /// <returns></returns>
        public bool MakeSureRecordSinger(int inWasteId)
        {
            try
            {


                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                PaperWasteRecords list = db.PaperWasteRecords.FirstOrDefault(p => p.Id == inWasteId);
                if (list.Status != (int)WasterPaperStatus.Sure)
                {
                    list.Status = (int)WasterPaperStatus.Sure;
                }
                db.SaveChanges();

            }
            }
            catch 
            {

                return false;
            }
            return true;

        }
    }
}
